--
-- Author: yawei.zhu
-- Date: 2018-10-09 20:31:32
--
-- BossRankView
--
local ListMenu = require("app.widget.ListMenu")
local FirstTaskTargetRender = require("app.task.views.renders.FirstTaskTargetRender")
local BossTeamRankRender = import(".renders.BossTeamRankRender")
local BossGuildRankRender = import(".renders.BossGuildRankRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.BossRankScene

function ClassRef:init()
	self._ui.Button_back:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)

	local function operate(op, data)
		if op == "btnClick" then
			self:selectIndex(data.index)
			executeFunc(self._onOperate, "selectedTarget", data)
		end
	end

	local params = {
		list = self._ui["Panel/ListView"],
		btnClass = FirstTaskTargetRender,
		subBtnClass = FirstTaskTargetRender, -- 暂时无用
		operate = operate,
		mustHaveTarget = true,
		onlyOpen = true,
        autoHideSecond = true,
	}
	self._menuList = ListMenu.new(params)

	self._listView = self._ui["Panel/Panel_r/ListView1"]

	-- 隐藏部分ui
	self._ui["Panel_r/Panel_page"]:setVisible(false)
	self._ui["Panel_r/Panel_top"]:setVisible(false)
	self._ui["Panel_r/Panel_top_1"]:setVisible(false)

    --放大镜按钮
    self.btnGotoSelf = self._ui["Panel_r/Button"]
    self.btnGotoSelf:addClickEventListener(function(sender)
        self:gotoSelf()
    end)

    self._ui["Panel_r/text"]:setString(L("bossRank_notOnTheList"))
	self._ui.Panel_tab:setVisible(false)
end

function ClassRef:refreshMenuList()
	local menuData = {}

	local temp = {}
	temp.index = 1
	temp.name = L("bossRank_teamKilled")
	temp.subDatas = {}
	menuData[1] = temp

	temp = {}
	temp.index = 2
	temp.name = L("bossRank_guildKilled")
	temp.subDatas = {}
	menuData[2] = temp

	self._menuList:refreshList(menuData)
end

function ClassRef:updateView(index)
	self._listView:removeAllChildren()
	self._selectIndex = index

	self._menuList:selectBtn(index)
end

function ClassRef:selectIndex(index)
	self._listView:removeAllChildren()
	self._selectIndex = index
	local panel = self._ui["Panel_r/Panel_top_team"]
	if index == 1 then
		panel:getChildByName("name"):setString(L("bossRank_player"))
		panel:getChildByName("combat"):setString(L("bossRank_teamPower"))
		panel:getChildByName("time"):setString(L("bossRank_killedTime"))
	elseif index == 2 then
		panel:getChildByName("name"):setString(L("bossRank_guildName"))
		panel:getChildByName("combat"):setString(L("bossRank_guildNums"))
		panel:getChildByName("time"):setString(L("bossRank_killedCount"))
	end
end

function ClassRef:updateTeamRank(teams)
	--dump(teams)
    self._listView:jumpToTop()
    self.selfNode = nil

	if self._selectIndex ~= 1 then return end
	self._listView:removeAllChildren()
	self._ui["Panel_r/text"]:setString(L("bossRank_notOnTheList"))
	if #teams <= 0 then
		self._ui.Panel_tips:setVisible(true)
		return
	end
	
    local myRank = 99
	for i, team in ipairs(teams) do
		local render = BossTeamRankRender.create()
		render:onBtnClick(self._onOperate)
		render:setData({i, team})
		self._listView:pushBackCustomItem(render)
        local teamInfo = team.teamInfo
        for _, member in ipairs(teamInfo.teamMembers) do
            if member.userBaseInfo.uid == UD:getUid() then
                self.selfNode = render
                --刷新自己的名次
                if myRank > i then
                    local timeData = os.date("*t", team.battleTime)
                    local szTime = string.format("%d:%02d:%02d", timeData.hour, timeData.min, timeData.sec)
                    myRank = i
                    self:onShowMySelf({time = szTime, rank = myRank})
                end
            end
        end
	end
    self._listView:refreshView() 
    self._ui.Panel_tips:setVisible(false)
end

-- 展开队伍信息
function ClassRef:onShowExtend(render)
	local items = self._listView:getItems()
	for _, item in ipairs(items) do
		if cc.isAlive(item) then
			if item ~= render then
				item:hideDetail()
			else
				item:showDetail()
			end
		end
	end

	self._listView:requestRefreshView()
	self._listView:forceDoLayout()
end

-- 收起队伍信息
function ClassRef:onHideExtend(render)
	render:hideDetail()
	self._listView:requestRefreshView()
	self._listView:forceDoLayout()
end

-- 显示我自己的数据
function ClassRef:onShowMySelf(data)
	local str = string.format(L("bossRank_myData"), data.time, data.rank)
	self._ui["Panel_r/text"]:setString(str)
end

-- 更新公会排名列表
function ClassRef:updateGuildRank(guilds)
	--dump(guilds)
    self._listView:jumpToTop()
    self.selfNode = nil

	if self._selectIndex ~= 2 then return end
	self._listView:removeAllChildren()
	self._ui["Panel_r/text"]:setString(L("bossRank_notOnTheList"))
	if #guilds <= 0 then
		self._ui.Panel_tips:setVisible(true)
		return
	end
    
	for i, guild in ipairs(guilds) do
		local render = BossGuildRankRender.create()
		render:onBtnClick(self._onOperate)
		render:setData({i, guild})
		self._listView:pushBackCustomItem(render)
	end

    self._listView:refreshView() 
    self._ui.Panel_tips:setVisible(false)

    -- 显示我自己公会的排名
    for i, guild in ipairs(guilds) do 
    	if guild.guildID == UD:getGuildId() then
    		local str = string.format(L("bossRank_guildData"), guild.killedCount, i)
    		self._ui["Panel_r/text"]:setString(str)
            local render = self._listView:getItems()[i]
            if not render then return end
            self.selfNode = render
    		break
    	end
    end
end

function ClassRef:gotoSelf()
    if not self.selfNode then
        print("selfNode is nil")
        return
    end
    self:gotoItem(self.selfNode)
end

function ClassRef:gotoItem(render)
    local off  = self._listView:getInnerContainerSize().height - self._listView:getContentSize().height
    if off > 0 then
        local off2 = math.max(0, off - (self._listView:getInnerContainerSize().height - math.abs(render:getPositionY() + render:getContentSize().height)))
		self._listView:jumpToPercentVertical(math.max(0, math.min(100, 100 * (1-off2/off))))
	end
	self:onShowExtend(render) -- 跳转到自己的队伍并展开
end

return ClassRef
